using System;
using System.Windows.Forms;
using WMPLib;
using NAudio.Wave;

namespace MusicPlayerApp
{
    public partial class Form1 : Form
    {
        private WindowsMediaPlayer windowsMediaPlayer;
        private WaveOutEvent waveOut;
        
        public Form1()
        {
            InitializeComponent();
            windowsMediaPlayer = new WindowsMediaPlayer();
            waveOut = new WaveOutEvent();
        }

        // 播放MP3文件
        private void PlayMP3(string filePath)
        {
            try
            {
                windowsMediaPlayer.URL = filePath;
                windowsMediaPlayer.controls.play();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error playing MP3 file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 播放ogg
        private void PlayOGG(string filePath)
        {
            try
            {
                var vorbisStream = new NAudio.Vorbis.VorbisWaveReader(filePath);
                waveOut.Init(vorbisStream);
                waveOut.Play();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error playing ogg file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 选择MP3
        private void btnOpenMP3_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "MP3 Files|*.mp3";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                PlayMP3(openFileDialog.FileName);
            }
        }

        // 选择ogg
        private void btnOpenOGG_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Ogg Files|*.ogg";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                PlayOGG(openFileDialog.FileName);
            }
        }

        // 停播
        private void btnStop_Click(object sender, EventArgs e)
        {
            windowsMediaPlayer.controls.stop();
            waveOut.Stop();
        }
    }
}
